/*
 * gy_enpclientfactory.cpp
 *
 *  Created on: 2015年9月15日
 *      Author: gaoyi
 */


#include "gy_enpclientfactory.h"

GYEnpClient* GYEnpClientFactory::Alloc()
{
	unsigned int seq = GetClientSeq();
	while (m_stMgr.find(seq) != m_stMgr.end())
	{
		seq = GetClientSeq();
	}

	m_stMgr[seq].m_uiSeq = seq;
	m_stMgr[seq].m_stNetHead.uiHandle = seq;
	m_stMgr[seq].m_stNetHead.uiHandleSeq = seq;

	return &m_stMgr[seq];
}

int GYEnpClientFactory::Free(GYEnpClient* pstClient)
{
	if (pstClient == NULL)
	{
		return -1;
	}
	return Free(pstClient->m_uiSeq);
}

int GYEnpClientFactory::Free(unsigned int seq)
{
	if (m_stMgr.find(seq) == m_stMgr.end())
	{
		return -1;
	}

	m_stMgr.erase(seq);
	return 0;
}

GYEnpClient *GYEnpClientFactory::Find(unsigned int seq)
{
	if (m_stMgr.find(seq) == m_stMgr.end())
	{
		return NULL;
	}

	return &m_stMgr[seq];
}

int GYEnpClientFactory::SetEmpty(unsigned int seq)
{
	if (m_stRemain.find(seq) != m_stRemain.end())
	{
		m_stRemain.erase(seq);
	}
	return 0;
}

int GYEnpClientFactory::SetRemain(unsigned int seq)
{
	if (m_stMgr.find(seq) == m_stMgr.end())
	{
		return -1;
	}

	if (m_stRemain.find(seq) == m_stRemain.end())
	{
		m_stRemain.insert(seq);
	}
	return 0;
}

bool GYEnpClientFactory::IsRemain(unsigned int seq)
{
	if (m_stRemain.find(seq) != m_stRemain.end())
	{
		return true;
	}
	return false;
}

int	GYEnpClientFactory::GetClientSize()
{
	return m_stMgr.size();
}

int	GYEnpClientFactory::GetRemainSize()
{
	return m_stRemain.size();
}
